﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aij1.Model;
using System.Text;
using Aij1.Utils;
using System.Web.UI;
using Aij1.Cms;
using Aij1.BLL;

namespace Aij1
{
    public class QBind
    {
        public static string GetArticlePage(object aType, object id)
        {
            if (aType == DBNull.Value)
            {
                aType = ArticleType.新闻;
            }
            switch ((ArticleType)aType)
            {
                case ArticleType.新闻: return String.Format("News_{0}.htm", id);
                case ArticleType.商品: return String.Format("ShopDetails_{0}.htm", id);
                case ArticleType.相册: return String.Format("Gallery_{0}.htm", id);
            }
            return "";
        }

        public static string GetCategoryPage(object aType, object id)
        {
            if (aType == DBNull.Value)
            {
                aType = ArticleType.新闻;
            }
            switch ((ArticleType)aType)
            {
                case ArticleType.新闻: return String.Format("NewsCenter_{0}.htm", id);
                case ArticleType.商品: return String.Format("Shop_{0}.htm", id);
                case ArticleType.相册: return String.Format("AlbumCenter_{0}.htm", id);
                default: return String.Format("NewsCenter_{0}.htm", id);
            }
        }

        public static string GetIntro(object intro)
        {
            string introStr = CommOp.CutStrWithDot(intro, 100);
            return String.IsNullOrEmpty(introStr) ? introStr : String.Format("<div>{0}</div>", introStr);
        }

        public static string GetIntro(object intro, object snapShotUrl)
        {
            Page page = HttpContext.Current.Handler as Page;
            string imgUrl = CommOp.ToStr(snapShotUrl);
            if (String.IsNullOrEmpty(imgUrl))
                return GetIntro(intro);
            else
            {
                string introStr = CommOp.CutStrWithDot(intro, 100);
                return String.Format("<div><img src='{0}' style='width:80px;height:60px; float:left;margin-right:1em;' />{1}</div>", page.ResolveClientUrl(imgUrl), introStr);
            }
        }

        public static string GetClass(object settopTime)
        {
            return (settopTime.GetType() == typeof(DBNull)) ? "li1" : "li2";
        }

        public static string GetKeywordsLink(A_Article art)
        {
            if (art == null || string.IsNullOrEmpty(art.Keywords)) return "";
            return "关键词: " + String.Join(" ", art.Keywords.Split(';').Select(s => String.Format("<a href='Search.aspx?Keyword={0}'>{1}</a>", HttpUtility.UrlEncode(s), s)).ToArray());
        }

        public static string GetCategoryLink(int cId, object cType, object oId, object cTitle)
        {
            if (cId != 0 || CommOp.ToInt(oId) == 0) return "";
            else return String.Format("[<a class='gay' href='{0}'>{1}</a>]&nbsp;", QBind.GetCategoryPage(cType, cId), cTitle);
        }

        static string albumFormat = @"<a href='Gallery_{0}.htm' target='_blank'>
<div style='width:{1}px;height:{2}px;overflow:hidden;margin-bottom:6px' >
<img src='{3}' border='0' style='margin-left:{4}px;margin-top:{5}px;width:{7}px;height:{8}px' alt='{6}' /></div>
{6}</a>";

        public static string GetAlbumHtml(A_Article album, int divW, int divH)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (album == null)
            {
                return String.Format(@"<a href='Shop.htm'>
                    <div style='width:{0}px;height:{1}px;overflow:hidden;margin-bottom:6px'>
<img src='images/waiting.jpg' style='border:0;width:{0}px;height:{1}px' alt='欢迎你加入' /></div>
                    欢迎你加入</a>", divW, divH);
            }

            //原图宽度和高度    
            int smallWidth;
            int smallHeight;

            string url = page.ResolveUrl(album.SamplePhotoId.SnapShotUrl);
            int width = album.SamplePhotoId.SnapShotWidth;
            int height = album.SamplePhotoId.SnapShotHeight;

            if (divW > AlbumData.SnapWidth)
            {
                 url = page.ResolveUrl(album.SamplePhotoId.Url);
                 width = album.SamplePhotoId.Width;
                 height = album.SamplePhotoId.Height;
            }
            
            //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)    
            if (((decimal)width) / height <= ((decimal)divW) / divH)
            {
                smallWidth = divW;
                smallHeight = divW * height / width;
            }
            else
            {
                smallWidth = divH * width / height;
                smallHeight = divH;
            }
            int marginLeft = (divW - smallWidth) / 2;
            int marginTop = (divH - smallHeight) / 2;
            return string.Format(albumFormat, album.Oid, divW, divH, url, marginLeft, marginTop, album.Title, smallWidth, smallHeight);
        }


        public static string GetTopNewsHtml(string catIds, int topN)
        {
            BasePage page = HttpContext.Current.Handler as BasePage;
            string format = @"<li><span class='orange'>·</span>
                <a href='{0}' title='{1}' target='_blank'>{1}</a> <span class='ind_time'>{2:MM-dd}</span></li>";

            StringBuilder sb = new StringBuilder();

            foreach (A_Article art in ArticleData.GetTopNews(page.XpoSession, catIds, topN))
            {
                sb.AppendFormat(format, GetArticlePage(art.AType, art.Oid), art.Title, art.DisplayTime);
            }
            return sb.ToString();
        }

    }
}